<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/15
 * Time: 14:47
 */
namespace App\Http\Middleware;

use App\Handlers\BackendApiResponse;
use Closure;

class BackendApiAuthMiddleware {

    use BackendApiResponse;

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next) {
        if (!isset($request->app_id) || empty($request->app_id)) {
            return $this->responseJson(1000000);//appId不能为空
        }
        if (!isset($request->sign) || empty($request->sign)) {
            return $this->responseJson(1000001);//签名不能为空
        }
        $appIds = array_keys(config('backendapikey'));
        if (!in_array($request->app_id, $appIds)) {
            return $this->responseJson(1000002);//非法客户端
        }
        $apiDebug = config('app.api_debug');
        if ($apiDebug) {
            return $this->responseJson(520);
        }
        return $next($request);
    }
}